import cv2
import mediapipe as mp


class MediaPipeProcessor:
    def __init__(self):
        # 初始化MediaPipe的手部模块
        self.mp_hands = mp.solutions.hands
        self.hands = self.mp_hands.Hands(
            static_image_mode=False,  # 非静态图像模式
            max_num_hands=2,  # 最多检测两只手
            min_detection_confidence=0.5,  # 检测置信度阈值
            min_tracking_confidence=0.5)  # 跟踪置信度阈值
        self.mp_drawing = mp.solutions.drawing_utils  # 绘制关键点和连线的工具

    def process_frame(self, frame):
        # 处理每一帧图像
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # BGR转RGB
        results = self.hands.process(frame_rgb)  # 使用MediaPipe处理图像

        # 如果检测到手部关键点，则绘制关键点和连线
        if results.multi_hand_landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                self.mp_drawing.draw_landmarks(
                    frame, hand_landmarks, self.mp_hands.HAND_CONNECTIONS,
                    self.mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4),
                    self.mp_drawing.DrawingSpec(color=(250, 44, 250), thickness=2))
        return frame
